package com.cqeec.demo.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cqeec.demo.bean.AdminAuth;
import com.cqeec.demo.bean.MenuTree;
import com.cqeec.demo.service.AdminAuthService;

/**
 * 系统菜单控制器类
 * 
 * @Title: AdminAuthController
 * @Descripton: 接收系统菜单信息并调用业务逻辑进行处理，然后返回信息给视图层
 */
@Controller
@RequestMapping("/system/menu")
public class AdminAuthController {
	// 菜单
	@Resource(name = "adminAuthService")
	private AdminAuthService adminAuthService;
	
	
	@RequestMapping("/sysMenuList")
	@ResponseBody
	public List<MenuTree> getSysMenu(HttpServletRequest request) {
		List<AdminAuth> list = adminAuthService.getSysMenu();
		List<MenuTree> sysMenuList = new ArrayList<MenuTree>();
		Iterator<AdminAuth> it_auth = list.iterator();
		/*根据easyui tree的json数据格式重新封装数据*/
		while(it_auth.hasNext())
		{
			AdminAuth auth = it_auth.next();
			MenuTree mt = new MenuTree();
			mt.setId(auth.getId());
			mt.setIconCls(auth.getIconCls());
			mt.setPid(auth.getPid());
			mt.setText(auth.getAuth_name());
			mt.setIframe(auth.getIframe());
			Map<String,String> m = new HashMap<String,String>();
			m.put("url", auth.getUrl());
			mt.setAttributes(m);
			String state = adminAuthService.hasChildrens(auth.getId()) ? "closed" : "open";
			mt.setState(state);
			sysMenuList.add(mt);
		}
		return sysMenuList;
	}
	@RequestMapping("/menuListByPage")
	@ResponseBody
	public Map<String, Object> getMenuListByPage(HttpServletRequest request,@RequestParam("id") Long id) {
		return adminAuthService.getMenuListByPage(id);
	}
}
